﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Drawing.Drawing2D;
using DevExpress.Utils.Extensions;

namespace Marmat.Template.WinForm
{
    public partial class TopMenu : UserControl
    {
        public TopMenu()
        {
            InitializeComponent();
            SetStyle(ControlStyles.ResizeRedraw, true); //尺寸变化后绘制
            SetStyle(ControlStyles.Opaque, false); //改善绘图质量
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.DoubleBuffer, true);
        }

        [DefaultValue(null)]
        [Description("Logo")]
        public Image Logo { get; set; }

        /// <summary>
        /// 鼠标点击菜单委托
        /// </summary>
        /// <param name="menuItem"></param>
        public delegate void MenuEventHandler(TopMenuItem menuItem);

        /// <summary>
        /// 鼠标点击菜单后执行
        /// </summary>
        [Description("鼠标点击菜单后执行")]
        public event MenuEventHandler MenuItemClick;

        public void AddControl(TopMenuItem item)
        {
            this.topMenItems.Add(item);
        }
        public void AddControl(IEnumerable<TopMenuItem> collection)
        {
            this.topMenItems.AddRange(collection);
        }
        private List<TopMenuItem> topMenItems = new List<TopMenuItem>();
        private Point mousePoint = new Point(-1, -1);
        private Dictionary<TopMenuItem, Rectangle> rectangles = new Dictionary<TopMenuItem, Rectangle>();
        private void TopMenu_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
            e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            if (this.Logo != null)
            {
                Rectangle _rect = new Rectangle(10, (this.Height - 55) / 2 + 4, 180, 53);
                e.Graphics.DrawImage(this.Logo, _rect);
            }

            //int top = 0;
            int interval = 8;//间距
            int image_width = 35;//头像宽度
            int itemWidth = 63;
            int width = (itemWidth - image_width) / 2;
            int x = 0 + 240 + 20;
            foreach (var item in this.topMenItems)
            {
                //菜单项的矩形
                Rectangle _rect = new Rectangle(x, e.ClipRectangle.Y, itemWidth, image_width + 20);
                if (_rect.Contains(this.mousePoint))
                {
                    Color FColor = Color.FromArgb(120, 220, 220, 220);
                    Brush b = new LinearGradientBrush(_rect, Color.Transparent, FColor, LinearGradientMode.Vertical);
                    e.Graphics.FillRectangle(b, _rect);
                }
                if (_rect.Contains(this.mousePoint) && this.isClick)
                {
                    Color FColor = Color.FromArgb(60, 20, 20, 20);
                    Brush b = new LinearGradientBrush(_rect, Color.Transparent, FColor, LinearGradientMode.Vertical);
                    e.Graphics.FillRectangle(b, _rect);
                }
                //图标的矩形
                if (item.Image != null)
                {
                    Rectangle rect = new Rectangle(x + width, e.ClipRectangle.Y, image_width, image_width);
                    e.Graphics.DrawImage(item.Image, rect);
                }
                using (Brush brush = new SolidBrush(Color.White))
                {
                    StringFormat format = new StringFormat();
                    format.FormatFlags = StringFormatFlags.NoClip;
                    format.Alignment = StringAlignment.Center;
                    Rectangle testRec = new Rectangle(x, e.ClipRectangle.Y + image_width + 2, itemWidth, 20);
                    e.Graphics.DrawString(item.Caption, this.Font, brush, testRec, format);
                }
                this.rectangles[item] = _rect;
                x += (itemWidth + interval);
            }
        }


        protected override void OnMouseClick(MouseEventArgs e)
        {
            foreach (var item in this.rectangles)
            {
                if (item.Value.Contains(e.Location))
                    this.MenuItemClick?.Invoke(item.Key);
            }
            base.OnMouseClick(e);
        }

        private bool isClick;
        protected override void OnMouseDown(MouseEventArgs e)
        {
            foreach (var item in this.rectangles)
            {
                if (item.Value.Contains(e.Location))
                {
                    this.isClick = true;
                    this.Invalidate();
                }
            }
            base.OnMouseDown(e);
        }

        protected override void OnMouseUp(MouseEventArgs e)
        {
            this.isClick = false;
            base.OnMouseUp(e);
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            this.mousePoint = e.Location;
            this.Invalidate();
            base.OnMouseMove(e);
        }
        protected override void OnMouseLeave(EventArgs e)
        {
            this.mousePoint = new Point(-1, -1);
            this.Invalidate();
            base.OnMouseLeave(e);
        }
    }

    /// <summary>
    /// 顶部菜单子项
    /// </summary>
    public class TopMenuItem
    {
        public string Name { get; set; }
        public string Caption { get; set; }
        public Image Image { get; set; }
    }
}
